package com.kaesun.ufit.weight;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;

import com.kaesun.ufit.hardware.Buzzer;
import com.kaesun.ufit.tools.ConfigData;
import com.kaesun.ufit.tools.ToolbarView;

public class ServiceActivity extends Activity implements View.OnClickListener, IOnHandlerMessage {

	private static final int BUTTON_IDS[] = {
		R.id.button_back, R.id.button_home,

		R.id.text_service_screensaver, R.id.button_service_screensaver,
		R.id.text_service_language, R.id.button_service_language,
		R.id.text_service_wifi, R.id.button_service_wifi,
		R.id.text_service_zerocalibration, R.id.button_service_zerocalibration,
		R.id.text_service_factoryreset, R.id.button_service_factoryreset
	};

	private static final int REQUEST_WIFI = 1;

	private static final int YESNO_ZERO_CALIBRATION = 1;
	private static final int YESNO_FACTORY_RERSET = 2;

	private MainApplication mApp = null;
	private WeakRefHandler mHandler = null;
	private ToolbarView mToolbarView = null;

	private ImageButton mScreenSaver = null;
	private ImageButton mLanguage = null;
	private AlertDialog mAlertDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_service);

		for (int i=0; i<BUTTON_IDS.length; i++) {
			findViewById(BUTTON_IDS[i]).setOnClickListener(this);
		}

		mApp = (MainApplication)getApplication();
		mHandler = new WeakRefHandler((IOnHandlerMessage)this);
		mToolbarView = (ToolbarView)findViewById(R.id.view_toolbar);

		mScreenSaver = (ImageButton)findViewById(R.id.button_service_screensaver);
		mLanguage = (ImageButton)findViewById(R.id.button_service_language);

		mToolbarView.chage2Button();
		mToolbarView.setTitle("Setup");

		if (true == ConfigData.getPrefBoolean(this, ConfigData.PREF_SERVICE_SCREENSAVER, false)) {
			mScreenSaver.setImageResource(R.drawable.btn_service_yes);
		} else {
			mScreenSaver.setImageResource(R.drawable.btn_service_no);
		}
		switch (ConfigData.getPrefInteger(this, ConfigData.PREF_SERVICE_LANGUAGE, ConfigData.DEF_SERVICE_LANGUAGE)) {
		case ConfigData.SERVICE_LANGUAGE_KOR:
			mLanguage.setImageResource(R.drawable.btn_service_kor);
			break;
		case ConfigData.SERVICE_LANGUAGE_ENG:
			mLanguage.setImageResource(R.drawable.btn_service_eng);
			break;
		}
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (null != mAlertDialog) {
			if (mAlertDialog.isShowing()) {
				mAlertDialog.dismiss();
			}
			mAlertDialog = null;
		}

		super.onPause();
	} 

	@Override
	protected void onResume() {
		mApp.setHandler(mHandler, true);

		super.onResume();
	}

	public void onClick(View arg0) {
		mApp.timeoutReset();

		Buzzer.delay(50);
		switch (arg0.getId()) {
		case R.id.button_back:
			setResult(RESULT_OK);
			finish();
			break;
		case R.id.button_home:
			setResult(RESULT_CANCELED);
			finish();
			break;

		case R.id.text_service_screensaver:
		case R.id.button_service_screensaver:
			if (true == ConfigData.getPrefBoolean(this, ConfigData.PREF_SERVICE_SCREENSAVER, false)) {
				mScreenSaver.setImageResource(R.drawable.btn_service_no);
				ConfigData.setPref(this, ConfigData.PREF_SERVICE_SCREENSAVER, false);
			} else {
				mScreenSaver.setImageResource(R.drawable.btn_service_yes);
				ConfigData.setPref(this, ConfigData.PREF_SERVICE_SCREENSAVER, true);
			}
			break;
		case R.id.text_service_language:
		case R.id.button_service_language:
			switch (ConfigData.getPrefInteger(this, ConfigData.PREF_SERVICE_LANGUAGE, ConfigData.DEF_SERVICE_LANGUAGE)) {
			case ConfigData.SERVICE_LANGUAGE_KOR:
				mLanguage.setImageResource(R.drawable.btn_service_eng);
				ConfigData.setPref(this, ConfigData.PREF_SERVICE_LANGUAGE, ConfigData.SERVICE_LANGUAGE_ENG);
				break;
			case ConfigData.SERVICE_LANGUAGE_ENG:
				mLanguage.setImageResource(R.drawable.btn_service_kor);
				ConfigData.setPref(this, ConfigData.PREF_SERVICE_LANGUAGE, ConfigData.SERVICE_LANGUAGE_KOR);
				break;
			}
			break;
		case R.id.text_service_wifi:
		case R.id.button_service_wifi:
			showActivity(REQUEST_WIFI);
			break;
		case R.id.text_service_zerocalibration:
		case R.id.button_service_zerocalibration:
			showYesNoDialog(YESNO_ZERO_CALIBRATION);
			break;
		case R.id.text_service_factoryreset:
		case R.id.button_service_factoryreset:
			showYesNoDialog(YESNO_FACTORY_RERSET);
			break;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mApp.timeoutReset();

		return super.onTouchEvent(event);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case REQUEST_WIFI:
			if (RESULT_OK != resultCode) {
				setResult(RESULT_CANCELED);
				finish();
			}
			break;
		}
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case MainApplication.HANDLER_TIMEOUT_SECOND:
			mToolbarView.setTime(msg.arg1, msg.arg2);
			break;
		case MainApplication.HANDLER_TIMEOUT_ACTIVITY:
			setResult(RESULT_OK);
			finish();
			break;
		}
	}


	private void showActivity(int req) {
		Intent intent = null;

		switch (req) {
		case REQUEST_WIFI:
			intent = new Intent(ServiceActivity.this, WiFiActivity.class);
			break;
		}

		if (null != intent) {
			startActivityForResult(intent, req);
		}
	}

	private void showYesNoDialog(int type) {
		if (null != mAlertDialog) {
			if (mAlertDialog.isShowing()) {
				mAlertDialog.dismiss();
			}
			mAlertDialog = null;
		}

		AlertDialog.Builder builder = null;
		switch (type) {
		case YESNO_ZERO_CALIBRATION:
			builder = new AlertDialog.Builder(this);
			builder.setTitle("Zero Calibration")
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mApp.hallSensorZero();
				}
			})
			.setNegativeButton("Cancel", null);
			break;
		case YESNO_FACTORY_RERSET:
			builder = new AlertDialog.Builder(this);
			builder.setTitle("Factory Reset")
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					ConfigData.factoryReset(ServiceActivity.this);
				}
			})
			.setNegativeButton("Cancel", null);
			break;
		}
		if (null != builder) {
			mAlertDialog = builder.create();
			mAlertDialog.show();
		}
	}
}
